<template>
  <div class="system-log-container">
    <div class="spaceForm">
      <el-form />
      <el-form>
        <el-button type="primary" @click="handleView">批量已读</el-button>
        <el-button type="danger" @click="handleDelete($event)">
          批量删除
        </el-button>
      </el-form>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" type="selection" />
      <el-table-column label="消息内容" prop="message_content">
        <template #default="{ row }">
          <el-badge :hidden="row.is_view == 1" is-dot>
            <div>[{{ row.message_title }}]{{ row.message_content }}</div>
          </el-badge>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="发送时间"
        prop="send_time_format"
        width="180px"
      />
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="85"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">详情</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-dialog
      title="详情"
      :visible.sync="dialogVisible"
      width="30%"
      @close="close"
    >
      <div>{{ detail.message_content }}</div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setView">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { mapActions } from 'vuex'
  import { getMessage, deleteMessage, setView } from '@/api/message'

  export default {
    name: 'Message',
    data() {
      return {
        list: [],
        listLoading: true,
        detail: {},
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        dialogVisible: false,
        queryForm: {
          page: 1,
          perpage: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      ...mapActions({
        getView: 'user/getView',
      }),
      setSelectRows(val) {
        this.selectRows = val
      },
      handleSizeChange(val) {
        this.queryForm.perpage = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      handleView() {
        if (this.selectRows.length > 0) {
          const ids = this.selectRows.map((item) => item.id).join()
          this.$baseConfirm('你确定要已读选中消息吗', null, async () => {
            const { data } = await setView({ message_ids: ids })
            if (data.is_success) {
              this.$baseMessage(
                '已读成功',
                'success',
                'vab-hey-message-success'
              )
              await this.fetchData()
            }
          })
        } else {
          this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
        }
      },
      async setView() {
        const { data } = await setView({ message_ids: this.detail.id })
        if (data.is_success) {
          this.fetchData()
        }
        this.close()
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前消息吗', null, async () => {
            const { data } = await deleteMessage({ message_ids: row.id })
            if (data.is_success) {
              this.$baseMessage(
                '删除成功',
                'success',
                'vab-hey-message-success'
              )
              await this.fetchData()
            }
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中消息吗', null, async () => {
              const { data } = await deleteMessage({ message_ids: ids })
              if (data.is_success) {
                this.$baseMessage(
                  '删除成功',
                  'success',
                  'vab-hey-message-success'
                )
                await this.fetchData()
              }
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleEdit(row) {
        this.detail = row
        this.dialogVisible = true
      },
      close() {
        this.detail = {}
        this.dialogVisible = false
      },
      queryData() {
        if (this.queryForm.searchDate) {
          console.log('has time')
          this.queryForm.start_time = this.queryForm.searchDate[0]
          this.queryForm.end_time = this.queryForm.searchDate[1]
        } else {
          this.queryForm.start_time = ''
          this.queryForm.end_time = ''
        }
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { items, total },
        } = await getMessage(this.queryForm)
        this.list = items
        this.total = total
        this.listLoading = false
        this.getView()
      },
    },
  }
</script>
<style lang="scss" scoped>
  ::v-deep sup.el-badge__content.is-fixed.is-dot {
    right: unset;
    left: -15px;
    top: 5px;
  }
</style>
